<?php
namespace App\Http\Controllers\Passenger\V2\Order;

use App\Enum\ActivityEnum;
use App\Http\Controllers\Controller;
use App\Http\Requests\PassengerOrderRequest;
use App\Models\Orders;
use App\Models\PayOrder;
use App\Models\RefundOrder;
use App\Models\UserCoupon;
use App\Services\PassengerActivityService;
use App\Services\PassengerOrderService;
use App\Services\PassengerPayService;
use App\Services\PassengerUserCouponService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Overtrue\LaravelWeChat\Facade;

/**
 * 乘客端用户订单相关
 * Class OrderController
 * @package App\Http\Controllers\Passenger\Order
 */
class OrderController extends Controller
{


    /**
     * 创建订单
     * @param PassengerOrderRequest $request
     * @param PassengerOrderService $service
     * @return OrderController|\Illuminate\Http\JsonResponse
     *
     */
    public function create(PassengerOrderRequest $request, PassengerOrderService $service)
    {

        $service->checkPending();

        //todo 重复操作验证
        $data = $request->all();

        $order_model = $service->createOrder($data['order_no'], $data['service_type_id']);

        return $this->sendJson([
            'id'                   => $order_model->id,
            'order_sn'             => $order_model->order_sn,
            'predict_price'        => $order_model->predict_price,
            'call_service_type_id' => $order_model->call_service_type_id,
//            'service_type_id'   => $order_model->service_type_id,
//            'service_type_name' => $order_model->serviceType->name,
            'origin_name'          => $order_model->distance->book_start,
            'destination_name'     => $order_model->distance->book_end,
            'status'               => $order_model->status,
        ]);

    }


    /**
     * 确认订单
     */
    public function confirm(Request $request)
    {

        $order_id   = (int) $request->post('id');

        $order_info = Orders::query()->where('status', '4')->find($order_id);

        if( !$order_info || $order_info->user_id != Auth::id())
            return $this->sendError('订单ID有误');


        if($order_info->real_price != $order_info-> real_should_price)
            return $this->sendError('请先处理补款或者退款！');

        DB::beginTransaction();

        $order_info->status = '5';
        $order_info->save();

        DB::commit();

        return $this->sendSuccess();
    }


    /**
     * 获取当前订单可使用的优惠券列表
     */
    public function getCouponList(Request $request, PassengerUserCouponService $service)
    {

        $order_id = (int) $request->get('id');
        $order_info = Orders::query()
            ->where('status', '4')
            ->where('user_id', Auth::id())
            ->find($order_id);
        if( !$order_info )
            return $this->sendError('订单ID有误');

        return $this->sendJson($service->getListAllByOrder($order_info->service_type_id, $order_info->cost_price, $order_info->distance->book_time));


    }





    /**
     * 取消订单
     * @param Request $request
     * @param PassengerOrderService $service
     * @return OrderController|\Illuminate\Http\JsonResponse
     */
    public function cancel(Request $request, PassengerOrderService $service)
    {

        $id   = $request->post('id');
        $from = $request->post('from', 1);
        $order_info = Orders::query()->where('id', $id)->where('user_id', Auth::id())->first();

        $may_status_arr = [1, 2]; //仅 1预约中，2已接单 状态能取消订单
        if( !$order_info || !in_array($order_info->status, $may_status_arr))
            return $this->sendError('取消订单失败!');

        if($from == 1 && $order_info->status ==2 ){
            return $this->sendError('司机已接单取消失败！');
        }

        $order = $service->cancel($order_info);

        if($order->cancel_price == 0 && $order->cancel_status == 2) { //待退款且无取消责任费  直接退款

            $pay_service = new PassengerPayService();
            $pay_service->refund($order);//发起退款

        }

        return $this->sendJson([
            'cancel_status' => $order->cancel_status,
        ]);

    }


    /**
     * 设置取消理由
     * @param Request $request
     * @return OrderController|\Illuminate\Http\JsonResponse
     */
    public function setCancelReason(Request $request)
    {

        $id     = $request->post('id');
        $reason = $request->post('reason');

        $order_info = Orders::query()->where('id', $id)->where('user_id', Auth::id())->first();

        if(!$order_info || $order_info->status != -1 || $order_info->cancel_reason)
            return $this->sendError('订单ID有误');

        $order_info->cancel_reason = $reason;
        $order_info->save();

        return $this->sendSuccess();

    }


    /**
     * 我的订单列表
     * @param Request $request
     * @param PassengerOrderService $service
     */
    public function getList(Request $request, PassengerOrderService $service)
    {

        $type     = $request->get('service_type_id', '');
        $per_page = $request->get('per_page', 15);
        $list     = $service->getPageByServiceType($type, $per_page);

        return $this->sendJson($list);
    }


    /**
     * 订单详情页
     */
    public function detail(Request $request, PassengerOrderService $service)
    {

        $id     = $request->get('id');
        $detail = $service->getDetail($id);

        return $this->sendJson($detail);

    }


    /**
     * 获取待处理订单
     */
    public function pending(PassengerOrderService $service)
    {

        $pending_order = $service->getPending();

        return $this->sendJson($pending_order);

    }





}
